// 验证子进程推迟，会给父进程发送那个信号

#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <string>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
void handler(int signum)
{
    std::cout << "get a signal :" << signum << " I am " << getpid() << std::endl;
    auto rid = ::waitpid(-1, nullptr, WNOHANG);

    if (rid > 0)
    {
        std::cout << "子进程退出成功， 回收成功, " << "子进程id:" << rid << std::endl;
    }
    else if (rid < 0)
    {
        std::cout << "回收完成" << std::endl;
    }
    else if (rid == 0)
    {
        std::cout << "退出的子进程已经被全部回收了" << std::endl;
    }
    else
    {
        std::cout << "未知错误" << std::endl;
    }
    // 现在需要对所有的子进程进行调度
}

int main()
{
    signal(SIGCHLD, handler);
    // 1. 1个子进程，如果是10个子进程呢？
    if (fork() == 0)
    {
        sleep(5);
        std::cout << "子进程推出" << std::endl;
        // std::cout << "child process: " << getpid() << std::endl;
    }

    while (true)
    {
        std::cout << "父进程: " << getpid() << std::endl;
        sleep(1);
    }

    return 0;
}